from .LeiDian import LeiDianMulator
from .MuMu import MuMuMulator

class MulatorManager:
    def __init__(self):
        """
        初始化模拟器管理器
        """
        self.mulator_instances = {}

    def init_mulator(self, mulator_type, install_path):
        """
        初始化指定类型的模拟器
        :param mulator_type: 模拟器类型 ('leidian' 或 'mumu')
        :param install_path: 模拟器安装路径
        :return: bool
        """
        try:
            if mulator_type.lower() == 'leidian':
                self.mulator_instances[mulator_type] = LeiDianMulator(install_path)
            elif mulator_type.lower() == 'mumu':
                self.mulator_instances[mulator_type] = MuMuMulator(install_path)
            else:
                raise ValueError(f"不支持的模拟器类型: {mulator_type}")
            return True
        except Exception as e:
            print(f"初始化模拟器失败: {e}")
            return False

    def get_all_mulator_info(self, mulator_type):
        """
        获取指定类型模拟器的所有实例信息
        :param mulator_type: 模拟器类型
        :return: 模拟器信息列表
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].get_all_mulator_info()
        return []

    def get_mulator_info(self, mulator_type, index):
        """
        获取指定类型和序号的模拟器信息
        :param mulator_type: 模拟器类型
        :param index: 模拟器序号
        :return: 模拟器信息字典
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].get_mulator_info(index)
        return None

    def execute_shell(self, mulator_type, index, command):
        """
        在指定模拟器中执行shell命令
        :param mulator_type: 模拟器类型
        :param index: 模拟器序号
        :param command: shell命令
        :return: (returncode, stdout, stderr)
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].execute_shell(index, command)
        return -1, "", "模拟器类型不存在"

    def is_app_installed(self, mulator_type, index, package_name):
        """
        检查指定应用是否已安装
        :param mulator_type: 模拟器类型
        :param index: 模拟器序号
        :param package_name: 应用包名
        :return: bool
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].is_app_installed(index, package_name)
        return False

    def launch_app(self, mulator_type, index, package_name):
        """
        启动指定应用
        :param mulator_type: 模拟器类型
        :param index: 模拟器序号
        :param package_name: 应用包名
        :return: bool
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].launch_app(index, package_name)
        return False

    def is_app_active(self, mulator_type, index, package_name):
        """
        检查指定应用是否处于前台活动状态
        :param mulator_type: 模拟器类型
        :param index: 模拟器序号
        :param package_name: 应用包名
        :return: bool
        """
        if mulator_type in self.mulator_instances:
            return self.mulator_instances[mulator_type].is_app_active(index, package_name)
        return False